package com.basic;

import java.util.LinkedList;
import java.util.Queue;

public class SearchTreeDepth {
	 public int minDepth(TreeNode root) {
		 if(root == null) return 0;
		 Queue<TreeNode> queue = new LinkedList<>();
		 queue.offer(root);
		 int step = 1;
		 while(!queue.isEmpty()) {
			for(int i = 0;i<queue.size();i++) {
				TreeNode node = queue.poll();
				 if(node.left == null && node.right == null)
					 return step;
		
				 if(root.left != null)
				 queue.offer(root.left);
				 if(root.right != null)
				 queue.offer(root.right);
			 	}
			step++;
			 }
		 return step;
	    }
}
class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}